<?PHP
include 'mysql.php';
include 'functions.php';
if(!isset($_GET['reg'])) {
	$_GET['reg'] = 'reg';
}
$email = $_POST['email'];    
$username = $_POST['username'];
$password = md5($_POST['password']);
$password2 = md5($_POST['repeat']);

// lets check to see if the username already exists

$checkuser = mysql_query("SELECT username FROM Users WHERE username='$username'");
$usernameexist = mysql_num_rows($checkuser); 
$checkemail = mysql_query("SELECT email FROM Users WHERE email='$email'"); 
$emailexist = mysql_num_rows($checkemail);

if($password == $password2) {
	if( $checkuser == 1 && $email == 1){
		unset($email);
		unset($username);
		header('location:/?a=register&reg=bad_unem');
		exit(); 
	} else if($checkuser == 1){
		unset($username);
		header('location:/?a=register&reg=bad_un');
		exit();
	} else if( $checkemail == 1){
		unset($email);
		header('location:/?a=register&reg=bad_em');
		exit(); 
	} else {
		$query = "INSERT INTO `Users`(`username`, `email`, `password`) VALUES ('$username', '$email', '$password')";
		mysql_query($query);
		header('location:/?a=register&reg=success');
	} 
} else {
	header('location:/?a=register&reg=bad_pw');
}
?>
